#include "mytcp.h"
#include "ui_mytcp.h"
#include <QLabel>
#include <QProcess>
#include <QDebug>
#include <QFile>
#include <QPixmap>
#include <QImage>
#include <QThread>
#include <QTime>
#include <QSize>



mytcp::mytcp(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::mytcp)
{
    ui->setupUi(this);

    //初始化套接字
    myserve = nullptr;
    mysocket=nullptr;

    //创建我的线程对象   赋予修改label的权利
    mythread = new tcpthread();
    mythread->showimage=(QLabel*)ui->image;
    mythread->thread_receive=ui->receive;




    //创建系统线程对象
    thread = new QThread(this);

    //将主线程和我编辑的线程捆绑
    mythread->moveToThread(thread);

    //IP和端口号的默认参数显示在输入栏中
    ui->IP_input->setText("192.168.4.2");
    ui->port_input->setText("8086");

    //等待处理数据的信号
    connect(this,&mytcp::to_deal_jpg_data_signal,mythread,&tcpthread::showjpg);

    setFocusPolicy(Qt::StrongFocus);
    installEventFilter(this);

}

//发送指令的函数
void mytcp::send_cmd(QString str)
{
    if(mysocket!=nullptr)
        mysocket->write(str.toUtf8().data());
}

//键盘按下的触发函数
void mytcp::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
        case Qt::Key_A :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"按下了 A "<<endl;
                    send_cmd("a_p");
                }
                break;

        case Qt::Key_S :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"按下了 S "<<endl;
                    send_cmd("s_p");
                }
                break;

        case Qt::Key_D :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"按下了 D "<<endl;
                    send_cmd("d_p");
                }
                break;

        case Qt::Key_W :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"按下了 W "<<endl;
                    send_cmd("w_p");
                }

                break;
        case Qt::Key_Shift :
                if(!event->isAutoRepeat())
                {

                    qDebug()<<"按下了 SHIFT "<<endl;
                    send_cmd("shift_p");
                }
                break;
        case Qt::Key_Space :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"按下了 space "<<endl;
                    send_cmd("space_p");
                }
                break;
    case Qt::Key_CapsLock :
            if(!event->isAutoRepeat())
            {
                qDebug()<<"按下了 CapsLock "<<endl;
                send_cmd("capslock_p");
            }
            break;
    case Qt::Key_Alt :
            if(!event->isAutoRepeat())
            {
                qDebug()<<"按下了 Alt "<<endl;
                send_cmd("alt_p");
            }
            break;

    }
}

//键盘松开的触发函数
void mytcp::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
        case Qt::Key_A :
            if(!event->isAutoRepeat())
            {
                send_cmd("a_r");
                qDebug()<<"松开了 A "<<endl;
            }
            break;

        case Qt::Key_S :
            if(!event->isAutoRepeat())
            {
                send_cmd("s_r");
                qDebug()<<"松开了 S "<<endl;
            }
            break;

        case Qt::Key_D :
            if(!event->isAutoRepeat())
            {
                send_cmd("d_r");
                qDebug()<<"松开了 D "<<endl;
            }
            break;

        case Qt::Key_W :
            if(!event->isAutoRepeat())
            {
                send_cmd("w_r");
                qDebug()<<"松开了 W "<<endl;
            }
            break;

        case Qt::Key_Shift :
            if(!event->isAutoRepeat())
            {
                qDebug()<<"松开了 SHIFT "<<endl;
                send_cmd("shift_r");
            }

            break;
        case Qt::Key_Space :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"松开了 space "<<endl;
                    send_cmd("space_r");
                }
                break;
        case Qt::Key_CapsLock :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"松开了 CapsLock "<<endl;
                    send_cmd("capslock_r");
                }
                break;
        case Qt::Key_Alt :
                if(!event->isAutoRepeat())
                {
                    qDebug()<<"松开了 Alt "<<endl;
                    send_cmd("alt_r");
                }
                break;
    }
}

mytcp::~mytcp()
{
    delete ui;
}

//读取tcp客户端缓冲区的信息
void mytcp::readdata()
{
    //启动线程
    thread->start();

    //够了5000字节再读
    if(mysocket->bytesAvailable()<length)
        return;

    QByteArray array;
    //放进待处理的集合中 QByteArray
    array.append(mysocket->readAll());

    emit to_deal_jpg_data_signal(array,length);
}

//有新的客户端连接进来
void mytcp::newconnection()
{
    mysocket=myserve->nextPendingConnection();
    connect(mysocket,&QTcpSocket::readyRead,this,&mytcp::readdata);
}


//ip  端口设置完成后  的确定键
void mytcp::on_pushButton_clicked()
{
    if(myserve!=nullptr)
        return;

    //获取IP地址和端口号
   QString IP=ui->IP_input->text();
   quint16 PORT =quint16( ui->port_input->text().toUInt());

   //指定父对象  自动回收空间
   myserve = new QTcpServer(this);

   //获取IP地址第三个参数   192.168.x.x  用来组成网关
   QStringList test = IP.split(".");
   QString gateway = "192.168."+test[2]+".1";

   //设置IP地址  子网掩码  网关
   QString set= "netsh interface ipv4 set address name = 小车 source = static address = " + IP+" mask = 255.255.255.0" + "gateway = "+gateway;
   QProcess p(this);
   p.start(set);
   p.waitForStarted();
   p.waitForStarted();

   //监听
   myserve->listen(QHostAddress::Any,PORT);

   //等待客户端连接
   connect(myserve,&QTcpServer::newConnection,this,&mytcp::newconnection);

}


//发送编辑区的信息
void mytcp::on_send_clicked()
{
   QString str = ui->textEdit->toPlainText();
   mysocket->write(str.toUtf8().data());
}
